package com.qf.servlet;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/s")
public class ServeltController {

    @RequestMapping("/test3")
    public void test3(HttpServletResponse response) throws Exception {

        System.out.println("=============");

        // 添加字符编码响应头
//        response.setContentType("text/html;charset=utf-8");

        // 向响应体中写入字符串
//        response.getWriter().println("hello servlet!!中国");

        response.sendRedirect("/login.html");

    }

    @RequestMapping("/analyze")
    public Map<String,Object> analyze(HttpServletRequest request){

        HashMap<String, Object> hashMap = new HashMap<>();

        Enumeration<String> parameterNames = request.getParameterNames();

        HashMap<String, Object> paramMap = new HashMap<>();
        while (parameterNames.hasMoreElements()) {
            String s = parameterNames.nextElement();
            String value = request.getParameter(s);
            System.out.println(s+"="+value);
            paramMap.put(s,value);
        }

        hashMap.put("param",paramMap);

        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headname = headerNames.nextElement();
            String value = request.getHeader(headname);
            System.out.println(headname+"="+value);
        }

        hashMap.put("heads",null);

        return hashMap;

    }



    @RequestMapping("/test2")
    public void test2(HttpServletRequest request){

        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String s = parameterNames.nextElement();
            String value = request.getParameter(s);
            System.out.println(s+"="+value);
        }

        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headname = headerNames.nextElement();
            String value = request.getHeader(headname);
            System.out.println(headname+"="+value);
        }

    }


    @RequestMapping("/test1")
    public HashMap<String,Object> test1(HttpServletRequest request){

        HashMap<String, Object> hashMap = new HashMap<>();

        String name = request.getParameter("name");
        String age = request.getParameter("age");

        String method = request.getMethod();
        String requestURI = request.getRequestURI();
        System.out.println(requestURI);
//        String contentType = request.getHeader("Content-Type");
        String host = request.getHeader("Host");
        System.out.println(host);
        String token = request.getHeader("token");
        System.out.println(token);


        String protocol = request.getProtocol();
        System.out.println(protocol);
        int serverPort = request.getServerPort();
        System.out.println(serverPort);

        hashMap.put("name",name);
        hashMap.put("age",age);
        hashMap.put("method",method);
        hashMap.put("requestURI",requestURI);
        hashMap.put("host",host);
        hashMap.put("protocol",protocol);
        hashMap.put("serverPort",serverPort);
        hashMap.put("token",token);

        return hashMap;
    }

}
